package org.gemstone.panel;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;

import org.come.Frame.ZhuFrame;
import org.come.entity.Goodstable;
import org.come.until.CutButtonImage;
import org.come.until.GoodsListFromServerUntil;
import org.come.until.MessagrFlagUntil;
import org.gemstone.btn.GemstoneOperationBtn;

import com.tool.btn.FormsOnOffBtn;
import com.tool.tcpimg.UIUtils;
import com.updateNew.MyIsif;

 

/**宝石操作主面板*/

/** 宝石操作主面板 */
public class GemstoneOperationMainPanel extends JPanel {

    private GemstoneOperationBtn syntherisCheckBtn // 合成按钮
            ,
            recastCheckBtn // 重铸按钮
            , appraisalCheckBtn // 鉴定按钮
            , helpBtn // 帮助按钮
            ;
    private int ys;
    private JLabel[] labels;
    private Goodstable[] goodsLabes = new Goodstable[2];

    /**
     * 对应的面板 1、合成面板 2、重铸面板 3、鉴定面板
     */
    private int typePanel = 1;
    private GemstoneOperationMainCardPanel gemstoneOperationMainCardPanel;

    public GemstoneOperationMainPanel() {

    		this.setPreferredSize(new Dimension(382, 445));
            this.setOpaque(false);
            this.setLayout(null);
            // 关闭按钮事件
            FormsOnOffBtn offBtn = new FormsOnOffBtn("inkImg/old/2/1.png", 1, 85);
            offBtn.setBounds(382 - 37, 10, 25, 25);
            this.add(offBtn);

            // 物品宝石操作
            class MListener extends MouseAdapter {
                private int p;

                public MListener(int p) {
                    this.p = p;
                }

                @Override
                public void mouseEntered(MouseEvent e) {
                    MessagrFlagUntil.setMouse(MessagrFlagUntil.MOUSE12);
                    // TODO Auto-generated method stub
                    Goodstable goodstable = GoodsListFromServerUntil.getGemstoneOperationSuit(p, 0);
                    if (goodstable != null) {
                        ZhuFrame.getZhuJpanel().creatgoodtext(goodstable);
                    }
                }

                @Override
                public void mouseExited(MouseEvent e) {
                    MessagrFlagUntil.setMouse(MessagrFlagUntil.MOUSE1);
                    // TODO Auto-generated method stub
                    ZhuFrame.getZhuJpanel().cleargoodtext();
                }

                @Override
                public void mouseClicked(MouseEvent e) {
                    // TODO Auto-generated method stub
                    for (int i = 0; i < labels.length; i++) {
                        labels[i].setBorder(null);
                    }
                    ((JLabel) e.getSource()).setBorder(BorderFactory.createLineBorder(Color.yellow));

                    Goodstable gemstoneOperationSuit = GoodsListFromServerUntil.getGemstoneOperationSuit(p, 0);
                    if (gemstoneOperationSuit == null) {
                        return;
                    }
                    int numTen = 0;
                    GemstoneOperationMainCardPanel gemstoneOperationMainCardPanel = GemstoneOperationMainFrame
                            .getGemstoneOperationMainFrame().getGemstoneOperationMainPanel()
                            .getGemstoneOperationMainCardPanel();

                    switch (typePanel) {
                    case 1:// 合成面板
                        for (int j = 0; j < goodsLabes.length; j++) {
                            if (goodsLabes[j] != null) {
                                numTen++;
                                continue;
                            }
                            if (!(gemstoneOperationSuit.getType().equals(744L)
                                    || gemstoneOperationSuit.getType().equals(746L)
                                    || gemstoneOperationSuit.getType().equals(749L)
                                    || gemstoneOperationSuit.getType().equals(752L)
                                    || gemstoneOperationSuit.getType().equals(755L)
                                    || gemstoneOperationSuit.getType().equals(758L)
                                    || gemstoneOperationSuit.getType().equals(761L)
                                    || gemstoneOperationSuit.getType().equals(764L) || gemstoneOperationSuit.getType()
                                    .equals(767L))) {
                                ZhuFrame.getZhuJpanel().addPrompt2("该宝石不能合成");
                                return;
                            }
                            int levelTwo = Integer.parseInt(cuttingString(gemstoneOperationSuit, 1));
                            if (levelTwo >= 10) {
                                ZhuFrame.getZhuJpanel().addPrompt2("宝石最高等级10级");
                                return;
                            }
                            if (goodsLabes[0] != null) {
                                int levelOne = Integer.parseInt(cuttingString(goodsLabes[0], 1));
                                // int levelTwo =
                                // Integer.parseInt(cuttingString(gemstoneOperationSuit,
                                // 1));
                                if (levelOne == levelTwo) {
                                    changeIcon(gemstoneOperationSuit, j, gemstoneOperationMainCardPanel
                                            .getGemstoneOperationSynthesisPanel().getGemstoneNumber());
                                    break;
                                } else {// 宝石等级不一致
                                    ZhuFrame.getZhuJpanel().addPrompt2("两颗宝石等级不一致");
                                    break;
                                }
                            } else if (goodsLabes[1] != null) {
                                int levelOne = Integer.parseInt(cuttingString(goodsLabes[0], 1));
                                // int levelTwo =
                                // Integer.parseInt(cuttingString(gemstoneOperationSuit,
                                // 1));
                                if (levelOne == levelTwo) {
                                    changeIcon(gemstoneOperationSuit, j, gemstoneOperationMainCardPanel
                                            .getGemstoneOperationSynthesisPanel().getGemstoneNumber());
                                    break;
                                } else {// 宝石等级不一致
                                    ZhuFrame.getZhuJpanel().addPrompt2("两颗宝石等级不一致");
                                    break;
                                }
                            } else {
                                changeIcon(gemstoneOperationSuit, j, gemstoneOperationMainCardPanel
                                        .getGemstoneOperationSynthesisPanel().getGemstoneNumber());
                                break;
                            }
                        }
                        if (numTen == 2) {
                            ZhuFrame.getZhuJpanel().addPrompt2("已经选择了两颗宝石");
                        }
                        break;

                    case 2:// 重铸面板
                        for (int j = 0; j < goodsLabes.length; j++) {
                            if (goodsLabes[j] != null) {
                                if (j == 0) {
                                    gemstoneOperationMainCardPanel.getGemstoneOperationAppraisalAndRecastPanel()
                                            .getReelectGoods().setType(goodsLabes[j].getType());
                                }
                                numTen++;
                                continue;
                            }
                            if (!(gemstoneOperationSuit.getType().equals(744L)
                                    || gemstoneOperationSuit.getType().equals(746L)
                                    || gemstoneOperationSuit.getType().equals(749L)
                                    || gemstoneOperationSuit.getType().equals(752L)
                                    || gemstoneOperationSuit.getType().equals(755L)
                                    || gemstoneOperationSuit.getType().equals(758L)
                                    || gemstoneOperationSuit.getType().equals(761L)
                                    || gemstoneOperationSuit.getType().equals(764L) || gemstoneOperationSuit.getType()
                                    .equals(767L))) {
                                ZhuFrame.getZhuJpanel().addPrompt2("该宝石不能重铸");
                                return;
                            }
                            if (goodsLabes[0] != null) {
                                int levelOne = Integer.parseInt(cuttingString(goodsLabes[0], 1));
                                int levelTwo = Integer.parseInt(cuttingString(gemstoneOperationSuit, 1));
                                if (levelTwo == levelOne - 4) {
                                    changeIcon(gemstoneOperationSuit, j, gemstoneOperationMainCardPanel
                                            .getGemstoneOperationAppraisalAndRecastPanel().getGemstoneNumber());
                                    break;
                                } else {
                                    ZhuFrame.getZhuJpanel().addPrompt2("材料宝石等级要比主宝石等级小4");
                                    break;
                                }
                            } else if (goodsLabes[1] != null) {
                                int levelTwo = Integer.parseInt(cuttingString(gemstoneOperationSuit, 1));
                                if (levelTwo >= 5) {
                                    changeIcon(gemstoneOperationSuit, j, gemstoneOperationMainCardPanel
                                            .getGemstoneOperationAppraisalAndRecastPanel().getGemstoneNumber());
                                    break;
                                } else {
                                    ZhuFrame.getZhuJpanel().addPrompt2("主宝石等级要大于等于5");
                                    break;
                                }
                            } else {
                                int levelTwo = Integer.parseInt(cuttingString(gemstoneOperationSuit, 1));
                                if (levelTwo >= 5) {
                                    changeIcon(gemstoneOperationSuit, j, gemstoneOperationMainCardPanel
                                            .getGemstoneOperationAppraisalAndRecastPanel().getGemstoneNumber());
                                    break;
                                } else {
                                    ZhuFrame.getZhuJpanel().addPrompt2("主宝石等级要大于等于5");
                                    break;
                                }
                                // changeIcon(gemstoneOperationSuit, j,
                                // GemstoneOperationAppraisalAndRecastPanel.getGemstoneOperationAppraisalAndRecastPanel().getGemstoneNumber());
                            }
                        }
                        if (numTen == 2) {
                            ZhuFrame.getZhuJpanel().addPrompt2("已经选择了两颗宝石");
                        }
                        break;
                    case 3:// 鉴定面板
                        for (int j = 0; j < goodsLabes.length; j++) {
                            if (goodsLabes[j] != null) {
                                numTen++;
                                continue;
                            }

                            if (goodsLabes[0] != null) {
                                if (!(gemstoneOperationSuit.getType().equals(744L)
                                        || gemstoneOperationSuit.getType().equals(746L)
                                        || gemstoneOperationSuit.getType().equals(749L)
                                        || gemstoneOperationSuit.getType().equals(752L)
                                        || gemstoneOperationSuit.getType().equals(755L)
                                        || gemstoneOperationSuit.getType().equals(758L)
                                        || gemstoneOperationSuit.getType().equals(761L)
                                        || gemstoneOperationSuit.getType().equals(764L) || gemstoneOperationSuit.getType()
                                        .equals(767L))) {
                                    ZhuFrame.getZhuJpanel().addPrompt2("该宝石不能添加到材料宝石");
                                    return;
                                }
                                int levelOne = Integer.parseInt(cuttingString(goodsLabes[0], 1));
                                int levelTwo = Integer.parseInt(cuttingString(gemstoneOperationSuit, 1));
                                if (levelTwo == levelOne - 3) {
                                    changeIcon(gemstoneOperationSuit, j, gemstoneOperationMainCardPanel
                                            .getGemstoneOperationAppraisalAndRecastPanel().getGemstoneNumber());
                                    break;
                                } else {
                                    ZhuFrame.getZhuJpanel().addPrompt2("材料宝石等级要比奇异宝石小3级");
                                    break;
                                }
                            } else if (goodsLabes[1] != null) {
                                if (gemstoneOperationSuit.getType().equals(770L)) {
                                    changeIcon(gemstoneOperationSuit, j, gemstoneOperationMainCardPanel
                                            .getGemstoneOperationAppraisalAndRecastPanel().getGemstoneNumber());
                                    break;
                                } else {
                                    ZhuFrame.getZhuJpanel().addPrompt2("要奇异宝石");
                                    break;
                                }
                            } else {
                                if (gemstoneOperationSuit.getType().equals(770L)) {
                                    changeIcon(gemstoneOperationSuit, j, gemstoneOperationMainCardPanel
                                            .getGemstoneOperationAppraisalAndRecastPanel().getGemstoneNumber());
                                    break;
                                } else {
                                    ZhuFrame.getZhuJpanel().addPrompt2("要奇异宝石");
                                    break;
                                }
                            }
                        }
                        if (numTen == 2) {
                            ZhuFrame.getZhuJpanel().addPrompt2("已经选择了两颗宝石");
                        }
                        break;

                    default:
                        break;
                    }
                    repaintBtn();
                }
            }

            syntherisCheckBtn = new GemstoneOperationBtn("skin/8100226/newbutton/K54.png", 1, 2);
            recastCheckBtn = new GemstoneOperationBtn("skin/8100226/newbutton/K55.png", 1, 4);
            appraisalCheckBtn = new GemstoneOperationBtn("skin/8100226/newbutton/K57.png", 1, 6);
            helpBtn = new GemstoneOperationBtn("skin/8100226/button/19.png", 1, UIUtils.COLOR_BTNTEXT, "?", UIUtils.TEXT_FONT, 0);
            labels = new JLabel[12];
            for (int i = 0; i < labels.length; i++) {
                labels[i] = new JLabel();
                labels[i].setOpaque(false);
                labels[i].setBounds(50 + i % 6 * 51, 308 + i / 6 * 51, 48, 48);
                MListener ml = new MListener(i);
                labels[i].addMouseListener(ml);
                this.add(labels[i]);
            }
            syntherisCheckBtn.setOpaque(false);
            recastCheckBtn.setOpaque(false);
            appraisalCheckBtn.setOpaque(false);
            helpBtn.setOpaque(false);

            syntherisCheckBtn.setBounds(48, 13, 75, 33);
            recastCheckBtn.setBounds(48+78, 13, 75, 33);
            appraisalCheckBtn.setBounds(48+78*2, 13, 75, 33);
            helpBtn.setBounds(338, 49, 17, 17);

//            this.add(helpBtn);
            this.add(syntherisCheckBtn);
            this.add(recastCheckBtn);
            this.add(appraisalCheckBtn);
            gemstoneOperationMainCardPanel = new GemstoneOperationMainCardPanel();
            this.add(gemstoneOperationMainCardPanel);


    }

    // 修改对应图片
    public void changeIcon(Goodstable goods, int num, JLabel[] labels) {
        goodsLabes[num] = goods;
        if (goods != null) {
            labels[num].setIcon(CutButtonImage.getGemstoneIcon(goods.getSkin(), 53, 51));
        } else {
            labels[num].setIcon(null);
        }
    }

    private ImageIcon iconBack;
    int num = 0;

    @Override
    protected void paintComponent(Graphics g) {
        // TODO Auto-generated method stub
        super.paintComponent(g);
        	if (iconBack == null) {
                iconBack = new ImageIcon("inkImg/newbackground/B265.png");
            }
            g.drawImage(iconBack.getImage(), 0, 0, 382, 445, this);
            GoodsListFromServerUntil.drawGemstoneOperationSuit(g, 0);

    }

    // 按钮刷新
    public void repaintBtn() {
    		switch (typePanel) {
            case 1:
                for (int k = 0; k < goodsLabes.length; k++) {
                    if (goodsLabes[k] == null) {
                        /** HGC-2019-12-11 */
                        gemstoneOperationMainCardPanel.getGemstoneOperationSynthesisPanel().getSynthesisBtn().setBtn(-1);
                        gemstoneOperationMainCardPanel.getGemstoneOperationSynthesisPanel().getSynthesisBtn()
                                .setIcon(CutButtonImage.getImage("skin/8100226/newbutton/aj1h.png", -1, -1));
                        return;
                    }
                }
                gemstoneOperationMainCardPanel.getGemstoneOperationSynthesisPanel().getSynthesisBtn().setBtn(1);
                gemstoneOperationMainCardPanel.getGemstoneOperationSynthesisPanel().getSynthesisBtn()
                        .setIcon(CutButtonImage.getImage("skin/8100226/newbutton/B20.png", -1, -1));
                break;
            case 2:
                for (int k = 0; k < goodsLabes.length; k++) {
                    if (goodsLabes[k] == null) {
                        gemstoneOperationMainCardPanel.getGemstoneOperationAppraisalAndRecastPanel().getReacastBtn()
                                .setBtn(-1);
                        gemstoneOperationMainCardPanel.getGemstoneOperationAppraisalAndRecastPanel().getReacastBtn()
                                .setIcon(CutButtonImage.getImage("skin/8100226/newbutton/aj1h.png", 59, 24));
                        gemstoneOperationMainCardPanel.getGemstoneOperationAppraisalAndRecastPanel().getReelectBtn()
                                .setBtn(-1);
                        gemstoneOperationMainCardPanel.getGemstoneOperationAppraisalAndRecastPanel().getReelectBtn()
                                .setIcon(CutButtonImage.getImage("skin/8100226/button/39.png", 68, 17));
                        return;
                    }
                }
                gemstoneOperationMainCardPanel.getGemstoneOperationAppraisalAndRecastPanel().getReacastBtn().setBtn(1);
                gemstoneOperationMainCardPanel.getGemstoneOperationAppraisalAndRecastPanel().getReacastBtn()
                        .setIcon(CutButtonImage.getImage("skin/8100226/newbutton/B20.png", 60, 78));
                gemstoneOperationMainCardPanel.getGemstoneOperationAppraisalAndRecastPanel().getReelectBtn().setBtn(1);
                gemstoneOperationMainCardPanel.getGemstoneOperationAppraisalAndRecastPanel().getReelectBtn()
                        .setIcon(CutButtonImage.getImage("skin/8100226/button/49.png", 68, 51));
                gemstoneOperationMainCardPanel.getGemstoneOperationAppraisalAndRecastPanel()
                        .getGemstoneOperationRecastTypePanel().repaintGemstoneIcon();

                break;
            case 3:
                for (int k = 0; k < goodsLabes.length; k++) {
                    if (goodsLabes[k] == null) {
                        gemstoneOperationMainCardPanel.getGemstoneOperationAppraisalAndRecastPanel().getReacastBtn()
                                .setBtn(-1);
                        gemstoneOperationMainCardPanel.getGemstoneOperationAppraisalAndRecastPanel().getReacastBtn()
                                .setIcon(CutButtonImage.getImage("skin/8100226/newbutton/aj1h.png", -1, -1));
                        return;
                    }
                }
                gemstoneOperationMainCardPanel.getGemstoneOperationAppraisalAndRecastPanel().getReacastBtn().setBtn(1);
                gemstoneOperationMainCardPanel.getGemstoneOperationAppraisalAndRecastPanel().getReacastBtn()
                        .setIcon(CutButtonImage.getImage("skin/8100226/newbutton/B20.png", 60, 78));
                break;

            default:
                break;
            }


    }

    // 切割字符串
    public static String cuttingString(Goodstable goods, int num) {
        if (goods.getType().equals(744L)) {
            return "1";
        }
        String value = goods.getValue();
        if (value == null) {
            return null;
        }
        String[] split = value.split("\\|");
        String[] split2 = split[num - 1].split("=");
        return split2[1];
    }

    public GemstoneOperationBtn getSyntherisCheckBtn() {
        return syntherisCheckBtn;
    }

    public void setSyntherisCheckBtn(GemstoneOperationBtn syntherisCheckBtn) {
        this.syntherisCheckBtn = syntherisCheckBtn;
    }

    public GemstoneOperationBtn getRecastCheckBtn() {
        return recastCheckBtn;
    }

    public void setRecastCheckBtn(GemstoneOperationBtn recastCheckBtn) {
        this.recastCheckBtn = recastCheckBtn;
    }

    public GemstoneOperationBtn getAppraisalCheckBtn() {
        return appraisalCheckBtn;
    }

    public void setAppraisalCheckBtn(GemstoneOperationBtn appraisalCheckBtn) {
        this.appraisalCheckBtn = appraisalCheckBtn;
    }

    public ImageIcon getIconBack() {
        return iconBack;
    }

    public void setIconBack(ImageIcon iconBack) {
        this.iconBack = iconBack;
    }

    public GemstoneOperationBtn getHelpBtn() {
        return helpBtn;
    }

    public void setHelpBtn(GemstoneOperationBtn helpBtn) {
        this.helpBtn = helpBtn;
    }

    public int getYs() {
        return ys;
    }

    public void setYs(int ys) {
        this.ys = ys;
    }

    public JLabel[] getLabels() {
        return labels;
    }

    public void setLabels(JLabel[] labels) {
        this.labels = labels;
    }

    public int getTypePanel() {
        return typePanel;
    }

    public void setTypePanel(int typePanel) {
        this.typePanel = typePanel;
    }

    public int getNum() {
        return num;
    }

    public void setNum(int num) {
        this.num = num;
    }

    public Goodstable[] getGoodsLabes() {
        return goodsLabes;
    }

    public void setGoodsLabes(Goodstable[] goodsLabes) {
        this.goodsLabes = goodsLabes;
    }

    public GemstoneOperationMainCardPanel getGemstoneOperationMainCardPanel() {
        return gemstoneOperationMainCardPanel;
    }

    public void setGemstoneOperationMainCardPanel(GemstoneOperationMainCardPanel gemstoneOperationMainCardPanel) {
        this.gemstoneOperationMainCardPanel = gemstoneOperationMainCardPanel;
    }

}
